<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Session{

	protected $_options = array();
	
	var $now ;
	
	var $ip;
	
	var $cookie;
	
	var $session_id;
	
	/**
	 * 构造函数
	 *
	 * @param array $config
	 */
	function __construct( $session_cookie = array() )
	{
		if( !empty( $session_cookie ) )
		{
			foreach ($session_cookie as $name => $value)
			{
				$this->_options[$name] = $value;
			}
		}
		else 
		{
			$this->_options = array
			(
					'sess_cookie_name' => 'suncms',
					'sess_expiration' => 7200,
					'sess_encrypt_cookie' => true,
					'sess_time_to_update' => 300,
					'cookie_prefix'	=> '',
					'cookie_domain' => '',
					'cookie_path' => '/'
			);
		}
		//获取当前时间
		$this->now = TIME;
		//获取session_id
		$this->session_id = session_id();
		//判断session生命周期是否为永久
		if ($this->_options['sess_expiration'] == 0)
		{
			$this->_options['sess_expiration'] = (60*60*24*365*2);
		}
		//设置session_cookie名
		$this->_options['sess_cookie_name'] = $this->_options['cookie_prefix'].$this->_options['sess_cookie_name'];
		//判断当前session并进行销毁或更新
		( $this->sess_expiration_check() )?$this->sess_update():$this->init_session();
	}
	
	/**
	 * 判断当前session是否有效
	 *
	 * @return bool
	 */
	function sess_expiration_check()
	{
		//判断是否有该cookie
		if( !isset( $_COOKIE[$this->_options['sess_cookie_name']] ) )
		{
			return FALSE;
		}
		$this->cookie = $_COOKIE[$this->_options['sess_cookie_name']];
		//判断是否需要解密
		if( $this->_options['sess_encrypt_cookie'] )
		{
			$this->cookie = authcode( $this->cookie );
		}
		//反序列化
		$this->cookie = unserialize( stripslashes( $this->cookie ) );
		//判断是否超时
		if( $this->cookie['last_activity']+$this->_options['sess_expiration'] < $this->now )
		{
			return FALSE;
		}
		//判断session_id是否匹配
 		if( $this->session_id !== $this->cookie['session_id'] )
 		{
			return FALSE;
		}
		return TRUE;
	}
	
	/**
	 * 更新cookie
	 * 
	 */
	function sess_update()
	{
		if( $this->cookie['last_activity']+$this->_options['sess_time_to_update'] < $this->now )
		{
			return;
		}
		$cookie_data = array
		(
				'session_id' => $this->session_id,
				'last_activity' => $this->now
		);
		$this->_set_cookie( $cookie_data );
		unset( $cookie_data );
	}
	
	
	/**
	 * 设cookie
	 *
	 * @param array $cookie_data
	 * @param string $type
	 * @return bool
	 */
	function _set_cookie( $cookie_data, $type = 'set' )
	{
		if( $type == 'unset' )
		{
			setcookie( $this->_options['sess_cookie_name'], '' );
			return true;
		}
		$cookie_data = serialize( $cookie_data );
		if ( $this->_options['sess_encrypt_cookie'] == TRUE )
		{
			$cookie_data = authcode( $cookie_data, ENCODE );
		}
		setcookie
		(
			$this->_options['sess_cookie_name'],
			$cookie_data,
			$this->_options['sess_expiration'] + $this->now,
			$this->_options['cookie_path'],
			$this->_options['cookie_domain'],
			0
		);
		return;
	}
	
	/**
	 * 获取一个session
	 *
	 * @param string $str
	 * @return string
	 */
	function get_session( $str )
	{
		if( !isset( $_SESSION[$str] ) )
		{
			return '';
		}
		$value = $_SESSION[$str];
		if( $this->_options['sess_encrypt_cookie'] )
		{
			$value = authcode( $value );
		}
		return $value;
	}
	
	/**
	 * 设session
	 *
	 * @param array $newdata
	 * @return bool
	 */
	function set_session( $newdata = array() )
	{
		$this->sess_update();
		if (count($newdata) > 0)
		{
			foreach ($newdata as $key => $val)
			{
				$_SESSION[$key] = ( $this->_options['sess_encrypt_cookie'] )?authcode( $val, 'ENCODE' ):$val;
			}
		}
		return true;
	}
	
	function init_session(){
		$this->sess_destroy();
		$cookie_data = array
		(
				'session_id' => $this->session_id,
				'last_activity' => $this->now
		);
		$this->_set_cookie( $cookie_data );
		unset( $cookie_data );
		
	}
	
	/**
	 * 注销一个session
	 *
	 * @param string $str
	 */
	function unset_session( $str )
	{
		unset( $_SESSION[$str] );
	}
	
	/**
	 * 销毁session
	 *
	 */
	function sess_destroy()
	{
		$this->_set_cookie( '', 'unset' );
		session_destroy();
	}
	
}